<template>
	<gradient-background>
		<view class="container">
			<view class="content">
				<!-- 页面标题 -->
				<view class="page-header">
					<view class="header-info">
						<text class="tool-icon">🚀</text>
						<text class="tool-title">事业占卜</text>
						<text class="tool-subtitle">事业发展和创业指导</text>
					</view>
				</view>
				
				<!-- 参数输入区域 -->
				<app-card>
					<view class="input-section">
						<text class="section-title">基本信息</text>
						
						<view class="form-item">
							<text class="form-label">事业类型</text>
							<picker 
								mode="selector" 
								:range="businessTypes" 
								:value="businessIndex"
								@change="onBusinessTypeChange"
								class="business-picker"
							>
								<view class="picker-display">
									<text class="picker-text">{{ displayBusinessType }}</text>
									<text class="picker-arrow">→</text>
								</view>
							</picker>
						</view>
						
						<view class="form-item">
							<text class="form-label">发展阶段</text>
							<picker 
								mode="selector" 
								:range="stageOptions" 
								:value="stageIndex"
								@change="onStageChange"
								class="stage-picker"
							>
								<view class="picker-display">
									<text class="picker-text">{{ displayStage }}</text>
									<text class="picker-arrow">→</text>
								</view>
							</picker>
						</view>
						
						<view class="form-item">
							<text class="form-label">关注方面</text>
							<view class="focus-selector">
								<view 
									v-for="focus in focusAreas" 
									:key="focus.value"
									class="focus-item"
									:class="{ active: params.focus === focus.value }"
									@tap="selectFocus(focus.value)"
								>
									<text class="focus-icon">{{ focus.icon }}</text>
									<text class="focus-text">{{ focus.name }}</text>
								</view>
							</view>
						</view>
						
						<view class="form-item">
							<text class="form-label">具体问题（可选）</text>
							<textarea 
								v-model="params.question" 
								placeholder="请描述您关心的具体事业问题"
								class="question-input"
								maxlength="200"
							></textarea>
						</view>
					</view>
				</app-card>
				
				<!-- 查询按钮 -->
				<view class="action-section">
					<app-button 
						:text="loading ? '占卜中...' : '开始占卜'" 
						type="primary"
						@tap="queryBusiness"
						:disabled="!canQuery || loading"
						:loading="loading"
					></app-button>
				</view>
				
				<!-- 结果显示区域 -->
				<app-card v-if="businessResult">
					<view class="result-section">
						<text class="section-title">事业分析</text>
						
						<view class="business-analysis">
							<view class="overall-business">
								<view class="business-score">
									<view class="score-circle">
										<text class="score-text">{{ businessResult.overall.score }}</text>
										<text class="score-unit">分</text>
									</view>
									<text class="score-level">{{ businessResult.overall.level }}</text>
								</view>
								<view class="business-desc">
									<text class="desc-text">{{ businessResult.overall.description }}</text>
								</view>
							</view>
							
							<view class="business-details">
								<view 
									v-for="(detail, index) in businessResult.details" 
									:key="index"
									class="detail-item"
								>
									<view class="detail-header">
										<text class="detail-aspect">{{ detail.aspect }}</text>
										<view class="detail-score">
											<text class="score-text">{{ detail.score }}分</text>
										</view>
									</view>
									<text class="detail-description">{{ detail.description }}</text>
								</view>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 发展建议 -->
				<app-card v-if="businessResult && businessResult.suggestions">
					<view class="suggestions-section">
						<text class="section-title">发展建议</text>
						
						<view class="suggestions-list">
							<view 
								v-for="(suggestion, index) in businessResult.suggestions" 
								:key="index"
								class="suggestion-item"
							>
								<view class="suggestion-icon">
									<text class="icon-text">💡</text>
								</view>
								<text class="suggestion-text">{{ suggestion }}</text>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 机遇分析 -->
				<app-card v-if="businessResult && businessResult.opportunities">
					<view class="opportunities-section">
						<text class="section-title">机遇分析</text>
						
						<view class="opportunities-list">
							<view 
								v-for="(opportunity, index) in businessResult.opportunities" 
								:key="index"
								class="opportunity-item"
							>
								<view class="opportunity-header">
									<text class="opportunity-title">{{ opportunity.title }}</text>
									<text class="opportunity-level">{{ opportunity.level }}</text>
								</view>
								<text class="opportunity-desc">{{ opportunity.description }}</text>
							</view>
						</view>
					</view>
				</app-card>
			</view>
		</view>
	</gradient-background>
</template>

<script>
import GradientBackground from '@/components/gradient-background/gradient-background.vue'
import AppCard from '@/components/app-card/app-card.vue'
import AppButton from '@/components/app-button/app-button.vue'
import ToolsService from '@/utils/tools-service.js'

export default {
	name: 'BusinessToolPage',
	components: {
		GradientBackground,
		AppCard,
		AppButton
	},
	data() {
		return {
			params: {
				businessType: '',
				stage: '',
				focus: '',
				question: ''
			},
			businessResult: null,
			loading: false,
			toolsService: null,
			businessIndex: 0,
			stageIndex: 0,
			businessTypes: [
				'科技创新', '电商零售', '餐饮服务', '教育培训', 
				'金融投资', '房地产', '制造业', '文化创意', 
				'健康医疗', '农业发展', '咨询服务', '其他行业'
			],
			stageOptions: [
				'创业筹备', '初创阶段', '成长期', '成熟期', '转型期'
			],
			focusAreas: [
				{ value: 'funding', name: '资金筹措', icon: '💰' },
				{ value: 'market', name: '市场开拓', icon: '📈' },
				{ value: 'team', name: '团队建设', icon: '👥' },
				{ value: 'product', name: '产品发展', icon: '🎯' },
				{ value: 'expansion', name: '规模扩张', icon: '🌍' },
				{ value: 'innovation', name: '创新突破', icon: '💡' }
			]
		}
	},
	computed: {
		canQuery() {
			return this.params.businessType && this.params.stage && this.params.focus
		},
		
		displayBusinessType() {
			return this.params.businessType || '选择事业类型'
		},
		
		displayStage() {
			return this.params.stage || '选择发展阶段'
		}
	},
	onLoad() {
		this.initData()
	},
	methods: {
		initData() {
			this.toolsService = new ToolsService()
		},
		
		onBusinessTypeChange(e) {
			this.businessIndex = e.detail.value
			this.params.businessType = this.businessTypes[e.detail.value]
		},
		
		onStageChange(e) {
			this.stageIndex = e.detail.value
			this.params.stage = this.stageOptions[e.detail.value]
		},
		
		selectFocus(focus) {
			this.params.focus = focus
		},
		
		async queryBusiness() {
			if (!this.canQuery) {
				uni.showToast({
					title: '请完善基本信息',
					icon: 'none'
				})
				return
			}
			
			this.loading = true
			
			try {
				const result = await this.toolsService.getBusinessInfo(this.params)
				
				if (result.success) {
					this.businessResult = result.data
					uni.showToast({
						title: '占卜完成',
						icon: 'success'
					})
				} else {
					throw new Error(result.message || '占卜失败')
				}
			} catch (error) {
				console.error('事业占卜失败:', error)
				uni.showToast({
					title: '占卜失败，请重试',
					icon: 'none'
				})
			} finally {
				this.loading = false
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	padding: 32rpx;
}

.content {
	padding-top: 40rpx;
}

.page-header {
	display: flex;
	align-items: center;
	margin-bottom: 40rpx;
	
	.header-info {
		flex: 1;
		
		.tool-icon {
			font-size: 64rpx;
			margin-bottom: 8rpx;
		}
		
		.tool-title {
			display: block;
			font-size: 40rpx;
			font-weight: bold;
			color: #000000;
			margin-bottom: 8rpx;
		}
		
		.tool-subtitle {
			display: block;
			font-size: 26rpx;
			color: rgba(0, 0, 0, 0.7);
		}
	}
}

.input-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.form-item {
		margin-bottom: 32rpx;
		
		&:last-child {
			margin-bottom: 0;
		}
		
		.form-label {
			display: block;
			font-size: 28rpx;
			color: #000000;
			margin-bottom: 16rpx;
		}
		
		.business-picker,
		.stage-picker {
			.picker-display {
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 88rpx;
				padding: 0 24rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				background-color: #fafafa;
				
				.picker-text {
					font-size: 28rpx;
					color: #000000;
				}
				
				.picker-arrow {
					font-size: 24rpx;
					color: rgba(0, 0, 0, 0.6);
				}
			}
		}
		
		.focus-selector {
			display: grid;
			grid-template-columns: repeat(3, 1fr);
			gap: 12rpx;
			
			.focus-item {
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 16rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				background-color: #fafafa;
				transition: all 0.3s ease;
				
				&.active {
					border-color: #FF9800;
					background: linear-gradient(135deg, #FF9800 0%, #F57C00 100%);
					
					.focus-icon,
					.focus-text {
						color: white;
					}
				}
				
				.focus-icon {
					font-size: 28rpx;
					margin-bottom: 8rpx;
				}
				
				.focus-text {
					font-size: 22rpx;
					color: #000000;
				}
			}
		}
		
		.question-input {
			width: 100%;
			min-height: 120rpx;
			padding: 16rpx;
			border: 2rpx solid #f0f0f0;
			border-radius: 12rpx;
			font-size: 28rpx;
			background-color: #fafafa;
			line-height: 1.5;
		}
	}
}

.action-section {
	margin: 48rpx 0;
}

.result-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.business-analysis {
		.overall-business {
			display: flex;
			align-items: center;
			margin-bottom: 32rpx;
			padding: 32rpx;
			background: linear-gradient(135deg, #FF9800 0%, #F57C00 100%);
			border-radius: 16rpx;
			
			.business-score {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-right: 32rpx;
				
				.score-circle {
					width: 120rpx;
					height: 120rpx;
					border: 4rpx solid rgba(255, 255, 255, 0.3);
					border-radius: 50%;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					margin-bottom: 12rpx;
					
					.score-text {
						font-size: 36rpx;
						font-weight: bold;
						color: white;
					}
					
					.score-unit {
						font-size: 20rpx;
						color: rgba(255, 255, 255, 0.8);
					}
				}
				
				.score-level {
					font-size: 24rpx;
					color: white;
					font-weight: 500;
				}
			}
			
			.business-desc {
				flex: 1;
				
				.desc-text {
					font-size: 28rpx;
					color: white;
					line-height: 1.6;
				}
			}
		}
		
		.business-details {
			display: flex;
			flex-direction: column;
			gap: 16rpx;
			
			.detail-item {
				padding: 20rpx;
				background-color: #f8f9fa;
				border-radius: 12rpx;
				
				.detail-header {
					display: flex;
					justify-content: space-between;
					align-items: center;
					margin-bottom: 12rpx;
					
					.detail-aspect {
						font-size: 28rpx;
						font-weight: 500;
						color: #000000;
					}
					
					.detail-score {
						.score-text {
							font-size: 24rpx;
							color: #FF9800;
							font-weight: 500;
						}
					}
				}
				
				.detail-description {
					font-size: 26rpx;
					color: rgba(0, 0, 0, 0.8);
					line-height: 1.5;
				}
			}
		}
	}
}

.suggestions-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.suggestions-list {
		.suggestion-item {
			display: flex;
			align-items: flex-start;
			margin-bottom: 20rpx;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.suggestion-icon {
				margin-right: 16rpx;
				margin-top: 4rpx;
				
				.icon-text {
					font-size: 32rpx;
				}
			}
			
			.suggestion-text {
				flex: 1;
				font-size: 28rpx;
				color: #000000;
				line-height: 1.6;
			}
		}
	}
}

.opportunities-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.opportunities-list {
		.opportunity-item {
			margin-bottom: 20rpx;
			padding: 20rpx;
			background-color: #f8f9fa;
			border-radius: 12rpx;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.opportunity-header {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 12rpx;
				
				.opportunity-title {
					font-size: 28rpx;
					font-weight: 500;
					color: #000000;
				}
				
				.opportunity-level {
					font-size: 24rpx;
					color: #FF9800;
					font-weight: 500;
				}
			}
			
			.opportunity-desc {
				font-size: 26rpx;
				color: rgba(0, 0, 0, 0.8);
				line-height: 1.5;
			}
		}
	}
}
</style>